//
//  AddressTableViewController.m
//  EncryptionTPB
//
//  Created by 吕剑锋 on 15/9/13.
//  Copyright © 2015年 coderjianfeng. All rights reserved.
//

#import "AddressTableViewController.h"
#import "AddressModel.h"
#import "AddressMessageViewController.h"
#import "UINavigationBar+Awesome.h"

#define CellHeight 64.f
#define AvatarWH 48.f
@interface AddressTableViewController ()
@property (strong, nonatomic) NSMutableArray *data;
@property (strong, nonatomic) NSArray *indexData;
@end

@implementation AddressTableViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"通讯录";
    [JFUtil setExtraCellLineHidden:self.tableView];
    //关闭横向纵向滑动条
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.navigationController.navigationBar setCustomBackgroundColor:[UIColor whiteColor]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark -Section高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 22;
}

#pragma mark -Section的Header的值
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *key = [self.indexData objectAtIndex:section];
    return key;
}

#pragma mark - Section header view
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *titleView = [[UIView alloc] init];
    titleView.backgroundColor = [UIColor customControllerBackgroundColor];//[UIColor colorWithRed:249.0/255.0 green:250.0/255.0 blue:251.0/255.0 alpha:1.0];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 320, 22)];
    label.backgroundColor = [UIColor clearColor];
    label.text = [self.indexData objectAtIndex:section];
    label.textColor = RGBACOLOR(84, 84, 84, 0.6);//[UIColor colorWithRed:144.0/255.0 green:165.0/255.0 blue:178.0/255.0 alpha:1.0];
    [titleView addSubview:label];
    return titleView;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.indexData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[self.data objectAtIndex:section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return CellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AddressCell *cell = [AddressCell cellForTableView:tableView];
    CNContact *contact = [[self.data objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

    if (contact.imageData!=nil) {
        cell.avatarImageView.image = [UIImage imageWithData:contact.imageData];
    }else{
        cell.avatarImageView.image = [UIImage imageNamed:@"icon-avatar"];
    }
    cell.nameLabel.text = [CNContactFormatter stringFromContact:contact style:CNContactFormatterStyleFullName];
    cell.descrptionLabel.text = [(CNPhoneNumber*)contact.phoneNumbers[0].value stringValue];
    cell.operatorLabel.text = @"南京";
    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CNContact *contact = [[self.data objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    AddressMessageViewController* messageViewController = [[AddressMessageViewController alloc] init];
    [messageViewController setHidesBottomBarWhenPushed:YES];
    messageViewController.tapContact = contact;
    [self.navigationController pushViewController:messageViewController animated:YES];
}

#pragma mark lazyload
- (NSMutableArray *)data{
    if (!_data) {
        _data = [[NSMutableArray alloc] init];
        AddressModel *model = [AddressModel new];
        NSArray *contacts = [model loadLocalAddressBook:self];
        if (contacts) {
            _data = [NSMutableArray arrayWithArray:contacts];
        }
    }
    return _data;
}

- (NSArray *)indexData{
    if (!_indexData) {
        _indexData = [[NSArray alloc] init];
        AddressModel *model = [AddressModel new];
        NSArray *indexArray = [model loadIndexAddressBook:self];
        if (indexArray) {
            _indexData = [NSArray arrayWithArray:indexArray];
        }
    }
    return _indexData;
}
@end

/**
 *  AddressCell
 */
@implementation AddressCell

+ (AddressCell *)cellForTableView:(UITableView*)tableView{
    static NSString *identifier = @"ADDRESSCELL";
    AddressCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
        cell = [[AddressCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        UIImageView *avatarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, CellHeight/2-AvatarWH/2, AvatarWH, AvatarWH)];
        avatarImageView.clipsToBounds = YES;
        avatarImageView.layer.cornerRadius = AvatarWH/2;
        [self.contentView addSubview:avatarImageView];
        self.avatarImageView = avatarImageView;

        TTTAttributedLabel *nameLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(25+AvatarWH, 10, SCREEN_WIDTH-(25+AvatarWH), 20)];
        nameLabel.font = [UIFont systemFontOfSize:16];
        nameLabel.textColor = [UIColor customGrayColor];
        [self.contentView addSubview:nameLabel];
        self.nameLabel = nameLabel;
        
        TTTAttributedLabel *descrptionLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(25+AvatarWH, CellHeight-8-20, SCREEN_WIDTH-(25+AvatarWH)-50, 20)];
        descrptionLabel.font = [UIFont systemFontOfSize:13];
        descrptionLabel.textColor = RGBACOLOR(84, 84, 84, 0.8);
        [self.contentView addSubview:descrptionLabel];
        self.descrptionLabel = descrptionLabel;
        
        TTTAttributedLabel *operatorLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-55, CellHeight-20, 50, 20)];
        operatorLabel.font = [UIFont systemFontOfSize:10];
        operatorLabel.textAlignment = NSTextAlignmentRight;
        operatorLabel.textColor = [UIColor customLightPlaceColor];
        [self.contentView addSubview:operatorLabel];
        self.operatorLabel = operatorLabel;
    }
    return self;
}
@end
